package com.demo.store.controller;

import com.demo.common.base.pojo.JsonRes;
import com.demo.common.email.pojo.MailInfo;
import com.demo.common.email.service.ISendMsgHandle;
import com.demo.common.ratelimit.annotation.ApiRateLimiter;
import com.demo.common.ratelimit.annotation.DistributeLock;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 测试类
 *
 * @author DayDayUp
 * create on 2023-08-24
 **/
@Slf4j
@RestController
@RequestMapping("store-admin")
public class TestController {

    @Resource
    private ISendMsgHandle iSendMsgHandle;

    @ApiRateLimiter
    @DistributeLock(waitTime = 50)
    @GetMapping("/distributeLock")
    public JsonRes<String> distributeLock() throws InterruptedException {
        log.info("执行前");
        Thread.sleep(10000);
        log.info("执行后");
        return JsonRes.ok();
    }


    @ApiRateLimiter(time = 2,count = 1)
    @GetMapping("/apiRateLimiter")
    public JsonRes<String> apiRateLimiter() {
        return JsonRes.ok();
    }

    @ApiRateLimiter
    @GetMapping("/apiRateLimiter1")
    public JsonRes<String> apiRateLimiter1() {
        return JsonRes.ok();
    }

    @ApiRateLimiter
    @GetMapping("/apiRateLimiter2")
    public JsonRes<String> apiRateLimiter2() {
        // 发送邮件
        MailInfo mailInfo = new MailInfo();
        mailInfo.setContent("你好呀");
        mailInfo.setReceiver(new String[]{"iamfxw@yeah.net"});
        mailInfo.setSubject("第一次我");
        iSendMsgHandle.sendSimpleTextEmail(mailInfo);
        return JsonRes.ok();
    }


    @GetMapping("/sendEnclosureEmail")
    public JsonRes<String> sendEnclosureEmail() {
        // 发送邮件
        MailInfo mailInfo = new MailInfo();
        mailInfo.setContent("你好呀");
        mailInfo.setReceiver(new String[]{"fxw7468@gmail.com"});
        mailInfo.setCc(new String[]{"fxw7468@qq.com"});
        mailInfo.setSubject("第一次我");
        // 图片
        Map<String, String> imageMap = new HashMap<>();
        imageMap.put("log", "C:\\Users\\001\\Desktop\\1.jpg");
        mailInfo.setImageMap(imageMap);
        mailInfo.setContent("<img src='cid:log' />");
        // 附件
        String[] attachStr = new String[]{"C:\\Users\\001\\Desktop\\1.jpg"};
        mailInfo.setAttachFileNames(attachStr);
        iSendMsgHandle.sendEnclosureEmail(mailInfo);
        return JsonRes.ok();
    }
}
